<template>
  <div class="register">

    <div class="r-div">
      <el-form :model="registerForm" ref="registerForm" label-width="70px" class="demo-dynamic">
        <el-form-item
            prop="phone"
            label="手机"
            :rules="[
      { required: true, message: '请输入手机号码', trigger: 'blur' },

    ]"
        >
          <el-input v-model="registerForm.phone"></el-input>
        </el-form-item>

        <el-form-item
            prop="nickname"
            label="昵称"
            :rules="[
      { required: true, message: '昵称不能为空', trigger: 'blur' },

    ]"
        >



          <el-input v-model="registerForm.nickname"></el-input>
        </el-form-item>
        <el-form-item
            prop="password"
            label="密码"
            :rules="[
      { required: true, message: '请输入密码', trigger: 'blur' },

    ]"
        >
          <el-input type="password" v-model="registerForm.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('registerForm')">提交</el-button>

          <el-button @click="resetForm('registerForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </div>


  </div>
</template>

<script>
import axios from "../utils/axios";

export default {
  data() {
    return {
      registerForm: {
        password: '',
        nickname:"",
        phone: ''
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {

          if(this.registerForm.phone.length ===11){
             axios({
               method:"POST",
               url:"/api/register",
               data:this.registerForm

             }).then(
                 (response)=>{
                 if(response.code==200){
                  this.$message.success("恭喜你注册成功")
                   this.$router.push("/login")
                 }else{
                   this.$message.error(`注册失败: ${response.msg}`)
                 }
                 }
             )




          }else{
            // this.$refs.rules[0].message="请输入正确的手机号码"
            this.$message.error("请输入正确的手机号码")
          }

        } else {
          alert('对不起注册失败！');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  }
}
</script>
<style scoped>
.register {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.6);
}

.r-div {
  padding-top:50px;
  width: 350px;
  border-radius: 5px 35px;
  height: 300px;
  background-color: white;

}

</style>